﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class Con_GoodsController : Controller
    {
        //
        // GET: /Con_Goods/
        private Con_GoodsInfo conInfo = new Con_GoodsInfo(true);

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取食材大类列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetType()
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("-1", "请选择...");
            dic.Add("1", "荤菜");
            dic.Add("3", "蔬菜");
            dic.Add("2", "鱼");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        public ActionResult Edit(string Id, int? ParentId)
        {
            ViewData["Type"] = GetType();
            ParentId = ParentId.HasValue ? ParentId : 0;
            Con_GoodsInfo info;
            Con_GoodsVM goodsVM = new Con_GoodsVM();
            int id = 0;
            if ((Id != null) && (Id != ""))
            {
                id = int.Parse(Id);
            }
            if (id > 0)
            {
                //string Condition = " and PriorID = " + id;
                info = conInfo.GetCon_GoodsById(id);
                if (info != null)
                {
                    Mapper.CreateMap<Con_GoodsInfo, Con_GoodsVM>();
                    goodsVM = Mapper.Map<Con_GoodsInfo, Con_GoodsVM>(info);
                }
                else
                {
                    goodsVM = new Con_GoodsVM();
                }
            }
            // 数据
            else if ((ParentId != null) && (ParentId != 0))
            {
                goodsVM = new Con_GoodsVM();
                goodsVM.PriorID = int.Parse(ParentId.ToString());
            }

            return View(goodsVM);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="goodsVM"></param>
        /// <param name="?"></param>
        /// <param name="Id"></param>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        public ActionResult SaveGoods(Con_GoodsVM goodsVM)
        {
            int flag = 0;
            Con_GoodsInfo goodsInfo;
            //if (ModelState.IsValid)
            //{
            if (goodsVM != null)
            {

                Mapper.CreateMap<Con_GoodsVM, Con_GoodsInfo>();
                goodsInfo = Mapper.Map<Con_GoodsVM, Con_GoodsInfo>(goodsVM);

                flag = conInfo.SaveCon_Goods(goodsInfo);
            }
            //}
            return Json(flag);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="pVM"></param>
        /// <returns></returns>
        public ActionResult GetCon_GoodsList(PageVM pVM)
        {
            int intParentId = 0;
            if (pVM.key.ToString() != "")
            {
                intParentId = int.Parse(pVM.key.ToString());
                if (intParentId == 0)
                {
                    //return Json(true);
                }
            }
            string strCondtion = "and PriorID = " + intParentId;
            int groupCount = conInfo.GetCon_GoodsCount(strCondtion);
            IList<Con_GoodsInfo> list = conInfo.GetAllCon_Goods(strCondtion, pVM.rp, pVM.page);
            JsonFlexiGridData flexigridObject = new JsonFlexiGridData();
            flexigridObject.page = pVM.page;
            flexigridObject.total = groupCount;
            List<string> v;
            foreach (Con_GoodsInfo ddi in list)
            {
                v = new List<string>();
                v.Add(ddi.Id.ToString());
                v.Add(ddi.GoodsNo);
                v.Add(ddi.GoodsName);
                v.Add(ddi.GoodsUnit);
                v.Add(ddi.PriorID.ToString());

                FlexiGridRow cell = new FlexiGridRow()
                {
                    id = ddi.Id.ToString(),
                    cell = v
                };
                flexigridObject.rows.Add(cell);
            }
            return Json(flexigridObject);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult QueryCon_Goods()
        {
            IList<Con_GoodsInfo> dmlist = conInfo.GetAllCon_Goods("and PriorID = 0");
            List<JsonTreeNode> nodes = new List<JsonTreeNode>();
            foreach (var item in dmlist)
            {
                JsonTreeNode cnode = new JsonTreeNode();
                cnode.id = item.Id.ToString();
                cnode.text = item.GoodsName;
                cnode.value = item.GoodsNo == null ? "" : item.GoodsNo;
                cnode.hasChildren = false;
                cnode.classes = "system";
                cnode.isexpand = false;
                cnode.complete = true;
                AddChildNode(cnode);
                nodes.Add(cnode);
            }
            return Json(nodes);
        }


        /// <summary>
        /// 递归添加子节点
        /// </summary>
        /// <param name="node"></param>
        private void AddChildNode(JsonTreeNode node)
        {
            int pid = Convert.ToInt32(node.id);
            IList<Con_GoodsInfo> mlist =
                conInfo.GetAllCon_Goods("").Where(cinfo => cinfo.PriorID == pid).ToList();
            if (mlist != null)
            {
                foreach (var Childitem in mlist)
                {
                    node.hasChildren = true;
                    JsonTreeNode nodeChild = new JsonTreeNode();
                    nodeChild.id = Childitem.Id.ToString();
                    nodeChild.text = Childitem.GoodsName;
                    nodeChild.value = Childitem.GoodsNo == null ? "" : Childitem.GoodsNo;
                    nodeChild.hasChildren = false;
                    nodeChild.isexpand = true;
                    AddChildNode(nodeChild);
                    node.ChildNodes.Add(nodeChild);
                }
            }

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DeleteCon_Goods(string GoodsIds)
        {
            bool flag = true;
            conInfo.DeleteCon_Goods(GoodsIds);
            return Json(flag);
        }

        /// <summary>
        /// 是否存在子节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult IsExistSubNode(int id)
        {
            bool flag = conInfo.IsExistSubNode(id);
            return Json(flag);
        }


    }
}
